<?php

namespace app\index\controller;



use app\admin\model\Dep_customer;
use think\Controller;
use think\Cookie;
use think\Request;
use think\Session;
use kuange\qqconnect\QC;
use app\index\controller\Base;
use Firebase\JWT\JWT;

class Login extends Base
{

    public function index()
    {

        return view('public/login');
    }


    public function doLogin(Request $request)
    {

        //获取值
        $data = input('post.');
        $auth = $data['auth'];
        $pwd = $data['pwd'];


        //判断用户用的什么身份登录
        $obj = model('customer')->where(function ($query) use ($auth) {
            $query->where('username',$auth)->where('status','1');
        })->whereOr(function ($query) use ($auth) {
            $query->where('tel',$auth)->where('status','1');
        })->whereOr(function ($query) use ($auth) {
            $query->where('email',$auth)->where('status','1');
        })->find();
        if(empty($obj)){
            $this->error('登陆失败，密码或用户名不存在',url('index/login/index'));
            return;
        }

        $customer = $obj->getData();
        $password = $customer['password'];


        //如果记住密码，生成token
        if(!empty($data['remember'])){
            $this->setToken($customer['id'],$auth);
        }

        //判断密码是否正确
        if(password_verify($pwd,$password)){
            $cus_dep = Dep_customer::get($customer['id'])->getData();
            $customer['nickname'] = $cus_dep['name'];
            $customer['figureurl'] = $cus_dep['headimg'];
            Session::set('user',$customer);
            $this->success('成功登陆',url('index/index/index'));
            return;
        }else{
            $this->error('登陆失败',url('index/login/index'));
            return;
        }
    }

    //设置token值
    public function setToken($id,$name)
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $key = "example_key";
        $token = array('id' => $id);
        $jwt = JWT::encode($token, $key);
        JWT::$leeway = 302400;
        $redis->set($name,$jwt);
        Cookie::set('token_jwt',$jwt,302400);

    }


    //跳转qq登录页面
    public function qqLogin()
    {
        $qc = new QC();
        return redirect($qc->qq_login());

    }

    //回调域
    public function qq()
    {
        $qc = new QC();

        $access_token = $qc->qq_callback();    // access_token
        $openid = $qc->get_openid();     // openid

        // 待处理用户逻辑
        $qc = new QC($access_token, $openid);
        $qq_user_info = $qc->get_user_info();
        Session::set('qq',$qq_user_info);
        //待处理
        $this->success('登录成功', url('/'));
    }

    //重置密码
    public function reset(){
        return view('public/reset');
    }

    //发送带TOKEN值得链接到用户邮箱
    public function getResetHttp(Request $request)
    {
        //$toemail='1228380958@qq.com';

        $toemail = $request->post('email');

        $obj = model('customer')->where(function ($query) use($toemail){
            $query->where('email',$toemail);
        })->find();
        if (!empty($obj)){
            $toemail = $obj->getData()['email'];
            $name = $obj->getData()['username'];

            $subject='我的生活个人博客密码找回中心';
            $content= "http://www.lyang.top/index/login/resetPwd?id=".password_hash($obj->getData()['id'],PASSWORD_DEFAULT);
            //Session::set('email',$toemail);
            $redis = new \Redis();
            $redis->connect('127.0.0.1', 6379);

            $redis->set('email', $toemail);
            send_mail($toemail,$name,$subject, $content);
            $this->success('链接已发送到你的邮箱，请查收！', url('index/index/index'));
        }else{
            $this->error('邮箱和注册时的邮箱不一致，请重新输入！', url('index/login/reset'));
        }
    }

    //加载重设密码页面
    public  function resetPwd(Request $request)
    {
        $strID = $request->get('id');
        Session::set('id',$strID);
        return view('public/resetpwd');
    }


    public function doReset()
    {
        $pwd = input('post.')['password'];
        $hashID = Session::get('id');

        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);

        $email = $redis->get('email');
        $obj = model('customer')->where(function ($query) use($email){
            $query->where('email',$email);
        })->find();

        if (!empty($obj)){
            $id = $obj->getData()['id'];
            //var_dump(password_verify($id, $hashID));die();
            if(password_verify($id, $hashID)){
                $obj->password = password_hash($pwd, PASSWORD_DEFAULT);
                if($obj->save() > 0){
                    Session::delete('id');
                    Session::delete('email');
                    $this->success('修改成功，请重新登录！', url('index/login/index'));
                }else{
                    $this->error('修改失败！', url('index/login/reset'));
                }
            }else{
                $this->error('无效连接！', url('index/index/index'));
            }
        }else{
            $this->error('连接已过期！','index/index/index');
        }
        return;

    }


}
